
#include "CommandBuilder.h"

struct CommandParameter
{
	std::wstring param;
	std::wstring value;

	CommandParameter(const std::wstring& p, const std::wstring& v) 
		: param(p), value(v)
	{
		
	}
};


CommandBuilder::CommandBuilder(const std::wstring& command, const std::wstring& subcmd)
{
	this->_command = command;
}

CommandBuilder::~CommandBuilder()
{
	for (ParametersList::const_iterator it=this->_parameters.begin(); it!=this->_parameters.end(); it++)
	{
		delete *it;
	}

	_parameters.clear();
}

void CommandBuilder::addParameter(const std::wstring& param, const std::wstring& value)
{
	this->_parameters.push_back(new CommandParameter(param, value));
}

std::wstring CommandBuilder::build() const
{
	std::wstring result = _command + BLANK_SPACE + _subcommand; 
	
	if (!_subcommand.empty()) 
	{
		result.append(BLANK_SPACE);
		result.append(_subcommand);
	}

	for (ParametersList::const_iterator it=this->_parameters.begin(); it!=this->_parameters.end(); it++)
	{
		result.append(BLANK_SPACE);
		result.append((*it)->param);

		if (!(*it)->value.empty()) 
		{
			result.append(BLANK_SPACE);
			result.append((*it)->value);
		} 
		
	}

	return result;
}